﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using System.Collections;

namespace RM2012Server.Controller
{
    public class TablesCtrl
    {
        TablesDAO m_TablesDAO = new TablesDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "TablesName";
            cmb.ValueMember = "TablesId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "TablesName";
            cmb.ValueMember = "TablesId";
            cmb.DataPropertyName = "TablesId";
            cmb.HeaderText = "Bàn";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb, int state)
        {
            cmb.DataSource = GetByState(state);
            cmb.DisplayMember = "TablesName";
            cmb.ValueMember = "TablesId";
            cmb.DataPropertyName = "TablesId";
            cmb.HeaderText = "Bàn";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "TablesName";
            cmb.ValueMember = "TablesId";
            cmb.DataPropertyName = "TablesId";
            cmb.HeaderText = "Bàn";
        }

        public ArrayList StateSource()
        {
            ArrayList list = new ArrayList();
            StateInfo s1 = new StateInfo(1, "Chưa sử dụng");
            StateInfo s2 = new StateInfo(2, "Đang sử dụng");
            StateInfo s3 = new StateInfo(3, "Đã đặt");
            StateInfo s4 = new StateInfo(4, "Ngừng sử dụng");
            list.Add(s1);
            list.Add(s2);
            list.Add(s3);
            list.Add(s4);
            return list;
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         ComboBoxEx cmbArea,
                         TextBoxX txtTablesId,
                         TextBoxX txtTablesName,
                         ComboBoxEx cmbState,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            cmbArea.DataBindings.Clear();
            cmbArea.DataBindings.Add("SelectedValue", bs, "AreaId");

            txtTablesId.DataBindings.Clear();
            txtTablesId.DataBindings.Add("Text", bs, "TablesId");

            txtTablesName.DataBindings.Clear();
            txtTablesName.DataBindings.Add("Text", bs, "TablesName");

            cmbState.DataBindings.Clear();
            cmbState.DataBindings.Add("SelectedValue", bs, "StateId");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         ComboBoxEx cmbArea,
                         TextBoxX txtTablesId,
                         TextBoxX txtTablesName,
                         ComboBoxEx cmbState,
                         TextBoxX txtRemark,
                         DataTable dtSource)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSource;

            cmbArea.DataBindings.Clear();
            cmbArea.DataBindings.Add("SelectedValue", bs, "AreaId");

            txtTablesId.DataBindings.Clear();
            txtTablesId.DataBindings.Add("Text", bs, "TablesId");

            txtTablesName.DataBindings.Clear();
            txtTablesName.DataBindings.Add("Text", bs, "TablesName");

            cmbState.DataBindings.Clear();
            cmbState.DataBindings.Add("SelectedValue", bs, "StateId");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_TablesDAO.GetAll();
        }

        public DataTable GetById(String tablesId)
        {
            return m_TablesDAO.GetById(tablesId);
        }

        public DataTable GetByArea(String areaId)
        {
            return m_TablesDAO.GetByArea(areaId);
        }

        public DataTable GetByState(int stateId)
        {
            return m_TablesDAO.GetByState(stateId);
        }

        public DataTable GetByLikeId(String tablesId)
        {
            return m_TablesDAO.GetByLikeId(tablesId);
        }

        public DataTable GetByName(String tablesName)
        {
            return m_TablesDAO.GetByName(tablesName);
        }

        public DataTable GetByOption(String tablesId, String tablesName, String areaId, int stateId)
        {
            return m_TablesDAO.GetByOption(tablesId, tablesName, areaId, stateId);
        }

        public int Add(TablesInfo info)
        {
            return m_TablesDAO.Add(info);
        }

        public int Update(TablesInfo info)
        {
            return m_TablesDAO.Update(info);
        }

        public int Delete(String tablesId)
        {
            return m_TablesDAO.Delete(tablesId);
        }

        public DataRow AddNewRow()
        {
            return m_TablesDAO.AddNewRow();
        }

        public void AddNewTables(DataRow m_Row)
        {
            m_TablesDAO.AddNewTables(m_Row);
        }

        public bool SaveData()
        {
            return m_TablesDAO.SaveData();
        }
    }
}
